#include "pathmanager.h"
#include <QDir>
#include <QDebug>
#include <QStandardPaths>
#include <QCoreApplication>
#include <QOperatingSystemVersion>

#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#endif

QString PathManager::getApplicationName()
{
    QString name = QCoreApplication::applicationName();
    return name.isEmpty() ? "LongImageReader" : name;
}

QString PathManager::getOrganizationName()
{
    QString org = QCoreApplication::organizationName();
    return org.isEmpty() ? "LimeReader" : org;
}

QString PathManager::getApplicationDisplayName()
{
#ifdef Q_OS_MACOS
    return "影谷长图处理大师";
#else
    return "LongImageReader";
#endif
}

QString PathManager::getConfigDirectory()
{
    // 检查是否为便携模式
    if (isPortableMode()) {
        return getPortableConfigDirectory();
    }

    QString appName = getApplicationName();
    QString orgName = getOrganizationName();

#ifdef Q_OS_WIN32
    // Windows: %APPDATA%/LongImageReader/
    QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    if (appData.isEmpty()) {
        // 备用方案
        appData = QDir::homePath() + "/AppData/Roaming/" + appName;
    }
    return appData;
#elif defined(Q_OS_MACOS)
    // macOS: ~/Library/Application Support/LongImageReader/
    QString appSupport = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    if (appSupport.isEmpty()) {
        // 备用方案
        appSupport = QDir::homePath() + "/Library/Application Support/" + appName;
    }
    return appSupport;
#else
    // Linux: ~/.config/LongImageReader/
    return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#endif
}

QString PathManager::getSettingsFilePath()
{
    return QDir(getConfigDirectory()).absoluteFilePath("settings.ini");
}

QString PathManager::getCacheDirectory()
{
    QString appName = getApplicationName();

#ifdef Q_OS_WIN32
    // Windows: %LOCALAPPDATA%/LongImageReader/Cache/
    QString localAppData = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    if (localAppData.isEmpty()) {
        // 备用方案
        localAppData = QDir::homePath() + "/AppData/Local/" + appName + "/Cache";
    }
    return localAppData;
#elif defined(Q_OS_MACOS)
    // macOS: ~/Library/Caches/com.limereader.longimagereader/
    QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
    if (cacheDir.isEmpty()) {
        // 备用方案
        cacheDir = QDir::homePath() + "/Library/Caches/com.limereader.longimagereader";
    }
    return cacheDir;
#else
    // Linux: ~/.cache/LongImageReader/
    return QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#endif
}

QString PathManager::getTempCacheDirectory()
{
    return QDir(getCacheDirectory()).absoluteFilePath("temp");
}

QString PathManager::getImageCacheDirectory()
{
    return QDir(getCacheDirectory()).absoluteFilePath("images");
}

QString PathManager::getThumbnailCacheDirectory()
{
    return QDir(getCacheDirectory()).absoluteFilePath("thumbnails");
}

QString PathManager::getLogDirectory()
{
    QString appName = getApplicationName();

#ifdef Q_OS_WIN32
    // Windows: %APPDATA%/LongImageReader/Logs/
    QString logDir = QDir(getConfigDirectory()).absoluteFilePath("Logs");
#elif defined(Q_OS_MACOS)
    // macOS: ~/Library/Logs/LongImageReader/
    QString logDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    logDir = QDir(logDir).absoluteFilePath("Logs/" + appName);
    if (logDir.isEmpty() || !QDir(logDir).exists()) {
        // 备用方案
        logDir = QDir::homePath() + "/Library/Logs/" + appName;
    }
#else
    // Linux: ~/.local/share/LongImageReader/logs/
    QString logDir = QDir(getConfigDirectory()).absoluteFilePath("logs");
#endif

    return logDir;
}

QString PathManager::getDebugLogPath()
{
    return QDir(getLogDirectory()).absoluteFilePath("debug.log");
}

QString PathManager::getCrashLogDirectory()
{
    return QDir(getLogDirectory()).absoluteFilePath("crashes");
}

QString PathManager::getTempDirectory()
{
    QString appName = getApplicationName();

#ifdef Q_OS_WIN32
    // Windows: %LOCALAPPDATA%/LongImageReader/Temp/
    QString tempDir = QDir(getCacheDirectory()).absoluteFilePath("Temp");
#elif defined(Q_OS_MACOS)
    // macOS: /tmp/LongImageReader/
    QString tempDir = QDir::temp().absoluteFilePath(appName);
#else
    // Linux: /tmp/LongImageReader/
    QString tempDir = QDir::temp().absoluteFilePath(appName);
#endif

    return tempDir;
}

QString PathManager::getTempFilePath(const QString& filename)
{
    return QDir(getTempDirectory()).absoluteFilePath(filename);
}

QString PathManager::getDocumentsDirectory()
{
    return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
}

QString PathManager::getDefaultExportDirectory()
{
    QString documentsDir = getDocumentsDirectory();
    return QDir(documentsDir).absoluteFilePath("LongImageReader_Exports");
}

QString PathManager::getPdfExportDirectory()
{
    return QDir(getDefaultExportDirectory()).absoluteFilePath("PDF");
}

QString PathManager::getUserDataDirectory()
{
    return QDir(getConfigDirectory()).absoluteFilePath("UserData");
}

QString PathManager::getBookmarksFilePath()
{
    return QDir(getUserDataDirectory()).absoluteFilePath("bookmarks.json");
}

QString PathManager::getRecentFilesPath()
{
    return QDir(getUserDataDirectory()).absoluteFilePath("recent_files.json");
}

QString PathManager::getApplicationDirectory()
{
    return QCoreApplication::applicationDirPath();
}

QString PathManager::getExecutablePath()
{
    return QCoreApplication::applicationFilePath();
}

bool PathManager::ensureDirectoryExists(const QString& path)
{
    QDir dir(path);
    if (!dir.exists()) {
        return dir.mkpath(".");
    }
    return true;
}

QString PathManager::cleanPath(const QString& path)
{
    return QDir::cleanPath(path);
}

bool PathManager::isPortableMode()
{
    // 检查应用程序目录中是否存在 portable.txt 文件
    QString portableFlag = QDir(getApplicationDirectory()).absoluteFilePath("portable.txt");
    return QFile::exists(portableFlag);
}

QString PathManager::getPortableConfigDirectory()
{
    // 便携模式：配置文件存储在应用程序目录下的 config 文件夹
    return QDir(getApplicationDirectory()).absoluteFilePath("config");
}

void PathManager::logAllPaths()
{
}

QString PathManager::getPlatformInfo()
{
#ifdef Q_OS_WIN32
    return "Windows";
#elif defined(Q_OS_MACOS)
    return "macOS";
#elif defined(Q_OS_LINUX)
    return "Linux";
#else
    return "Unknown";
#endif
}